home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.acns.nwu.edu!ftpbox!mothost!news
- From: bhowes@cssun3.corp.mot.com (Brad Howes)
- Newsgroups: comp.lang.c++
- Subject: Type Converter Problem
- Date: 22 Jan 1996 23:34:20 GMT
- Organization: Motorola, Inc.
- Distribution: world
- Message-ID: <BHOWES.96Jan22163420@cssun3.corp.mot.com>
- Reply-To: bhowes@cssun3.corp.mot.com
- NNTP-Posting-Host: cssun3.corp.mot.com
-
- After running the code below, I get A::active == -1, and B::active == 0. Anyone
- with an explaination why? Or is my compiler bugged...
-
- class A {
- long a;
-
- public:
- static long active; // keep track of all A objects in existence
- A( int x = 0 ) : a( x ) { ++active; };
- ~A() { --active; };
- }
-
- class B {
- char b[ 4 ];
-
- public:
- static long active; // keep track of all B objects in existence
- B( char *x = "\0\0\0\0" ) { memcpy( b, x, 4 ); ++active; };
- ~B() { --active; };
- //
- // Converter to A class object
- //
- operator A() { return A(((b[0]*256+buf[1])*256+buf[2])*256+buf[3]); };
- }
-
- long A::active = 0;
- long B::active = 0;
-
- static void
- f( void ) {
- B b( "FOOL" ); // calls B::B()
- A a( b ); // calls B::operator A() and then A::A(
- //
- // ??? Should be 1 and 1, right???
- //
- cout << "number of B objects: " << B::active << '\n';
- cout << "number of A objects: " << A::active << '\n';
- }
-
-
- int
- main( int argc, char *argv[] )
- {
- f();
- //
- // ??? Should be 0 and 0. I get 0 and -1.
- //
- cout << "number of B objects: " << B::active << '\n';
- cout << "number of A objects: " << A::active << '\n';
- }
-
- Thanks,
-
- Brad
- +---------------------------------------------------------------------------+
- | Brad Howes | Motorola E-Mail: XBH001 |
- | EMT Development | Internet: bhowes@cssun3.corp.mot.com |
- | Motorola Corporate - MD H1780 | xbh001@email.mot.com |
- | 8111 E. McDowell Rd. | Voice: 602 441 1522 Fax: 602 441 5455 |
- | Scottsdale, AZ 85257-3806 | Pager: 602 519 4227 |
- +---------------------------------------------------------------------------+
- --
- +---------------------------------------------------------------------------+
- | Brad Howes | Motorola E-Mail: XBH001 |
- | EMT Development | Internet: bhowes@cssun3.corp.mot.com |
- | Motorola Corporate - MD H1780 | xbh001@email.mot.com |
- | 8111 E. McDowell Rd. | Voice: 602 441 1522 Fax: 602 441 5455 |
- | Scottsdale, AZ 85257-3806 | Pager: 602 519 4227 |
- +---------------------------------------------------------------------------+
-